package com.example.yuejianapp14.Activity.Model;

import com.example.yuejianapp14.Activity.JavaBean.class1;
import com.example.yuejianapp14.Activity.Listener.ClassInfoListener;
import com.example.yuejianapp14.Activity.Listener.ClassListener;
import com.example.yuejianapp14.Activity.Servics.ClassService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 张森 on 2017/9/25.
 */

public class ClassModel {
    private Retrofit retrofit;
    private String BASEURL ="http://10.0.2.2/YueJian/index.php/Api/Class/";
    private String HeadPhotoNameDefault="user_headphoto.png";

    public ClassModel(){
        retrofit= new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }




    public void getClassList(final ClassListener listener){
        ClassService classServic=retrofit.create(ClassService.class);
       Call<List<class1>> call=classServic.getclass();
        call.enqueue(new Callback<List<class1>>() {
            @Override
            public void onResponse(Call<List<class1>> call, Response<List<class1>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<List<class1>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });


    }
    public void findClass(String clubid,final ClassListener listener){
        ClassService classServic=retrofit.create(ClassService.class);
        Call<List<class1>> call=classServic.findClass(clubid);
        call.enqueue(new Callback<List<class1>>() {
            @Override
            public void onResponse(Call<List<class1>> call, Response<List<class1>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<class1>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }


    public void classinfo (String classid,final ClassInfoListener listener){
        ClassService classServic=retrofit.create(ClassService.class);
        Call call = classServic.classinfo(classid);
        Callback<class1> callback =new Callback<class1>() {
            @Override
            public void onResponse(Call<class1> call, Response<class1> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("onresponse fail");

            }

            @Override
            public void onFailure(Call<class1> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }



    public void selectclass (String flag,final ClassListener listener){
        ClassService classServic=retrofit.create(ClassService.class);
        Call call = classServic.selectclass(flag);
        Callback<List<class1>> callback =new Callback<List<class1>>() {
            @Override
            public void onResponse(Call<List<class1>> call, Response<List<class1>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("onresponse fail");

            }

            @Override
            public void onFailure(Call<List<class1>> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }



}
